package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.web.coupon.entity.Config;
import com.wangsd.web.coupon.mapper.ConfigMapper;
import com.wangsd.web.coupon.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-15
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    @Resource
    ConfigMapper configMapper;

    @Override
    public IPage<Config> selectPage(Integer pageNum, Integer pageSize, Config config) {
        Page<Config> page = new Page<>(pageNum, pageSize);
        IPage<Config> resultIPage = configMapper.selectListXml(page, config);
        return resultIPage;
    }

    @Override
    public void addOrUpdate(Config entity) {
        if (entity.getId() != null) {
            configMapper.updateById(entity);
        } else {
            configMapper.insert(entity);
        }
    }

    @Override
    public List<Config> selectConfigListByCompanyId(Long companyId) {
        QueryWrapper<Config> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        queryWrapper.orderByDesc("config_order");
        List<Config> configCompanies = configMapper.selectList(queryWrapper);
        return configCompanies;
    }
}
